<template>
  <div class="xiecheng">
    <div class="admin_main_block">
      <div class="admin_main_block_top">
        <div class="admin_main_block_left">
          <div>{{ i18n('keyword.begin.from') }}</div>
        </div>

        <div class="admin_main_block_right">
          <div>
            <el-button icon="Back" @click="router.go(-1)">
              {{ i18n('common.return') }}
            </el-button>
          </div>
        </div>
      </div>

      <div class="admin_form_main">
        <el-form label-width="120px" ref="formRef" :model="dataForm" :rules="dataRule">
          <el-form-item :label="i18n('keyword.title')" prop="value">
            <el-select v-model="dataForm.value" :placeholder="i18n('common.please_select') + i18n('keyword.title')" :remote-method="loadHarborList" :loading="loading" reserve-keyword filterable remote clearable>
              <el-option v-for="(v, k) in harborList" :label="v.chinese_name" :key="k" :value="v.id">
              </el-option>
            </el-select>
          </el-form-item>

          <el-form-item :label="i18n('keyword.total')" prop="total">
            <el-input-number :placeholder="i18n('common.please_input') + i18n('keyword.total')" v-model="dataForm.total"></el-input-number>
          </el-form-item>

          <el-form-item :label="i18n('common.status')" prop="status">
            <el-switch
              v-model="dataForm.status"
              active-value="1"
              :active-text="i18n('common.enable')"
              inactive-value="2"
              :inactive-text="i18n('common.disable')">
            </el-switch>
          </el-form-item>

          <el-form-item>
            <el-button
              v-if="isAuth('module:keyword:begin:handle')"
              type="primary"
              @click="dataFormSubmit(formRef)">
              {{ i18n('common.confirm') }}
            </el-button>
            <el-button @click="resetForm(formRef)">
              {{ i18n('common.reset') }}
            </el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup name="keyword-begin-form">
  import { useBase } from "@/hooks/base/useBase"
  import { useForm } from "@/hooks/form/useForm"

  const { router, query, i18n, isAuth } = useBase()

  const model = ref('keyword')

  const harborList = ref([])
  const loading = ref(false)

  const dataForm = reactive({
    id: 0,
    type: 2,
    value: '',
    total: 0,
    status: '1',
  })

  const dataRule = reactive({
    value: [{
      required: true,
      message: i18n('keyword.rules.value.require'),
      trigger: 'blur',
    }],
  })

  const {
    formRef,
    dataFormSubmit,
    resetForm
  } = useForm(model.value, dataForm)

  const init = (form) => {
    dataForm.id = query.id || 0

    nextTick(() => {
      form.value.resetFields()
      if (dataForm.id) {
        $http({
          url: $http.adornUrl(`/keyword/view/${dataForm.id}`),
          method: 'get',
          params: $http.adornParams({}),
        }).then(({ data }) => {
          if (data && data.status === 200) {
            dataForm.value = data.data.value
            dataForm.total = data.data.total
            dataForm.status = data.data.status.value + ''
          }
        }).then(() => {
          if (dataForm.value) {
            $http({
              url: $http.adornUrl('/business/harbor/select'),
              method: 'get',
              params: $http.adornParams({
                id: dataForm.value
              }),
            }).then(({ data }) => {
              if (data && data.status === 200) {
                harborList.value = data.data
              }
            })
          }
        })
      }
    })
  }

  const loadHarborList = (name) => {
    if (name) {
      loading.value = true
      $http({
        url: $http.adornUrl('/business/harbor/select'),
        method: 'get',
        params: $http.adornParams({
          english_name: name
        }),
      }).then(({ data }) => {
        if (data && data.status === 200) {
          harborList.value = data.data
          loading.value = false
        } else {
          $message.error(data.message)
        }
      })
    } else {
      harborList.value = []
    }
  }

  onMounted(() => {
    init(formRef)
  })
</script>
